<template>
  <el-dialog draggable overflow :title="titleMap[mode]" v-model="visible" :width="1024" destroy-on-close
    @closed="$emit('closed')">
    <!-- <sc-water-mark ref="wm" text="已审核" subtext="誉卓纱线"> -->
    <sk-tabs type="border-card">
      <el-tab-pane ref="tab" label="基础信息">
        <el-divider content-position="left">基本信息</el-divider>
        <el-form :model="form" :rules="rules" ref="dialogForm" label-width="70px" label-position="right">
          <el-row align="bottom">
            <el-col :span="7" align="bottom">
              <el-form-item label="单据日期" prop="billdate">
                <el-date-picker v-model="form.billdate" type="date" placeholder="选择日期" format="YYYY-MM-DD"
                  value-format="YYYY-MM-DD" />
                <!-- <el-input label="单据日期" v-model="form.code" clearable></el-input> -->
              </el-form-item>
            </el-col>
            <el-col :span="7" align="bottom">
              <el-form-item label="编号" prop="code">
                <el-input label="编号" v-model="form.code" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" clearable></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-divider content-position="left">库存信息</el-divider>
        <sc-VxeTable ref="save" :gridOps="gridOps" :data="form.details" :gridEvts="gridEvts"
          :tablecode="'storage_action_custallocation_save'">
        </sc-VxeTable>
      </el-tab-pane>
      <el-tab-pane label="附件">
        <sk-upload v-model="form.attachments" :limit="10" draggable tip="单个文件不要超过10M,请上传图像格式文件">
        </sk-upload>

        <el-dialog v-model="dialogVisible">
          <img w-full :src="dialogImageUrl" alt="Preview Image" />
        </el-dialog>
      </el-tab-pane>
    </sk-tabs>
    <!-- </sc-water-mark> -->
    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
  <skBrowserChoose v-model="openChooseDlg" :name="'storage_browser'" @close="openChooseDlg = false" @select="additems">
  </skBrowserChoose>
</template>

<script>

import skBrowserChoose from "@/components/skBrowserChoose";
import tool from '@/utils/tool';
import XEUtils from "xe-utils";
export default {
  emits: ["success", "closed"],
  components: { skBrowserChoose },
  data() {
    return {
      mode: "add",
      dialogImageUrl: "",
      dialogVisible: false,
      openChooseDlg: false,
      gridOps: {
        columns: [
          {
            field: "auxiliary_quantity",
            event: {
              keyup: ({ row }) => {
                row["quantity"] = row["auxiliary_quantity"] * 25;
              },
            },
          },
        ],
        height: 300,
      },
      gridEvts: {
        choose: () => (this.openChooseDlg = true),
      },
      titleMap: {
        add: "新增",
        edit: "编辑",
      },

      form: {
        id: "",
        billdate: XEUtils.toDateString(new Date(), "yyyy-MM-dd"),
        code: "",
        remark: "",
        details: [],
        attachments: [],
      },
      rules: { billdate: { required: true, message: "请选择单据日期" } },
      visible: false,
      isSaveing: false,
    };
  },
  mounted() { },
  methods: {
    //显示
    open(mode = "add") {
      this.mode = mode;
      this.visible = true;
      return this;
    },
    //表单提交方法
    submit() {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true;

          var res = await this.$API.storage.custAllocation.save.post(this.form);
          this.isSaveing = false;
          if (res.code == 200) {
            this.$emit("success", this.form, this.mode);
            this.visible = false;
            this.$message.success("操作成功");
          } else {
            this.$alert(res.message, "提示", { type: "error" });
          }
        }
      });
    },
    async getItems(id) {
      var res = await this.$API.storage.custAllocation.info.get({
        filter: [{ id: id }],
      });
      if (res.code == 200) {
        Object.assign(this.form, tool.removeNull(res.data));
      }
    },
    additems(items) {
      this.openChooseDlg = false;
      items.map((row) => {
        row["oldcompany_name"] = (row.customer?.name) ?? 'YZ';
        row["oldcompany_id"] = row.customer_id;
        row["customer_id"] = row.company_id;
        row["quantity"] = row.available_quantity;
        row["auxiliary_quantity"] = row.available_auxiliary_quantity;
        delete row.id;
        delete row.company_id;
        delete row.company;
        this.form.details.push(row);
        return row;
      });
    },
    //表单注入数据
    setData(data) {
      this.getItems(data.id);
    },
  },
};
</script>

<style></style>
